package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    //攻略
    @Autowired
    private StrategyRepository strategyRepository;
    //攻略分类
    @Autowired
    private IStrategyCatalogService catalogService;
    //攻略主题
    @Autowired
    private IStrategyThemeService themeService;
    //目的地
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Override
    public void save(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public Strategy findByOne(String id) {
        return strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> findAll() {
        return strategyRepository.findAll();
    }

    //高查
    @Override
    public Object query(StrategyQuery qo) {
        Query query = new Query();
        long count = mongoTemplate.count(query, Strategy.class);
        if (count==0) {
            return Page.empty();
        }
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("subTitle").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getThemeId())) {
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        count = mongoTemplate.count(query, Strategy.class);
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageRequest);
        List<Strategy> list = mongoTemplate.find(query, Strategy.class);
        return new PageImpl<>(list,pageRequest,count);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {

        //目的地id
        //目的地名字
        StrategyCatalog strategyCatalog = catalogService.findByOne(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());

        //分类名称
        strategy.setCatalogName(strategyCatalog.getName());
        //主题名称
        StrategyTheme strategyTheme = themeService.findByOne(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //是否国外
        List<Destination> destinations = destinationService.queryToasts(strategy.getDestId());
        if (destinations!=null&&destinations.size()>0) {
            //是中国设置成ABROAD_NO(数值0),反之是国外设置成ABROAD_YES(数值1)
            strategy.setIsabroad(
                    "中国".equals(destinations.get(0))
                            ?Strategy.ABROAD_NO:
                            Strategy.ABROAD_YES);
        }

        if (StringUtils.hasLength(strategy.getId())) {
            Strategy queryStrategy = this.findByOne(strategy.getId());
            strategy.setCreateTime(new Date());
            strategy.setCreateTime(queryStrategy.getCreateTime());
            strategy.setViewnum(queryStrategy.getViewnum());
            strategy.setReplynum(queryStrategy.getReplynum());
            strategy.setFavornum(queryStrategy.getFavornum());
            strategy.setSharenum(queryStrategy.getSharenum());
            strategy.setThumbsupnum(queryStrategy.getThumbsupnum());

            this.update(strategy);
        } else {
            //时间
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }
    }

    /**
     * 根据攻略分类外键查询点击量前三数据
     * @param destId
     * @return
     */
    @Override
    public List<Strategy> queryStrategyViewnumTop3ByDestId(String destId) {
        PageRequest pageRequest = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy> strategies = strategyRepository.findByDestId(destId,pageRequest);
        return strategies;
    }

    @Override
    public List<Strategy> findByCatalogId(String catalogId) {
        return strategyRepository.findByCatalogId(catalogId);
    }

    @Override
    public JsonResult changeHotValue(String id, String hot) {
        return null;
    }

    //更新攻略统计数据(点赞,评论数,点赞数...)
    @Override
    public void updateStrategyStatisDataByStrategyVO(StrategyStatisVO vo) {
        if (vo==null) {
            return;
        }
        Query query = new Query();
        //指定修改的文档对象(条件)
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        //设置修改的数据
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("sharenum",vo.getSharenum());
        update.set("favornum",vo.getFavornum());
        update.set("replynum",vo.getReplynum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        //修改操作
        mongoTemplate.updateMulti(query,update,Strategy.class);
    }
}
